//
//  DiscoverViewController.m
//  HAODE
//
//  Created by panquanReabam.com on 15/9/8.
//  Copyright (c) 2015年 reabam.com. All rights reserved.
//

#import "DiscoverViewController.h"
#import "DiscoverSupplyListViewController.h"
#import "DiscoverHouseholdListViewController.h"
#import "RDVTabBarController.h"
#import "MyViewController.h"
#import "HAODEsingleton.h"
#import "DailyTableViewCell.h"
#import "TheLoginViewController.h"

#import "SelectTheCertificationViewController.h"
#import "HttpRequest.h"
#import "RDVTabBarController.h"
#import "MJRefresh.h"

#import "DiscoverMCircleListViewController.h"
#import "DiscoverBusinessListViewController.h"
#import "DiscoverTrainListViewController.h"
#import <CoreLocation/CoreLocation.h>

#import <CoreLocation/CLLocationManagerDelegate.h>
@interface DiscoverViewController ()<UITableViewDataSource,UITableViewDelegate,UIScrollViewDelegate,CLLocationManagerDelegate, UITextViewDelegate,UIImagePickerControllerDelegate,UIAlertViewDelegate,UIGestureRecognizerDelegate,UINavigationControllerDelegate,UIActionSheetDelegate>



{
    MJRefreshHeaderView* _header;
    MJRefreshFooterView* _footer;
    NSArray *array;
    NSArray *array1;
    NSArray *array2;
    NSArray *array3;
    UIButton *noticeCountBtn;
    UIWebView *web;
    UIButton* name1;
    UIButton* name;
    HAODEsingleton* commdity;
    UIButton *imageView ;
    UIView *headerView;
    UIButton* renzhenButton;
    NSDictionary *DataLine;
    UIButton *button;
    UIButton*TheMessageBtn;
    NSString* userAudit;
}
@property (strong, nonatomic) CLLocationManager *locManager;

@property (strong, nonatomic) NSString *currentLatitude; //纬度
@property (strong, nonatomic) NSString *currentLongitude; //经度
@property (nonatomic, strong) CLLocationManager *locationManager;
@end

@implementation DiscoverViewController
@synthesize alt = alt;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"发现";
        
    }
    return self;
}
- (void)viewDidDisappear:(BOOL)animated

{
    
    [super viewDidDisappear:animated];
    
    
    
    self.navigationController.delegate = nil;
    
}
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    // 禁用 iOS7 返回手势
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    }
}
-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    
   commdity=[HAODEsingleton shared];
    [[self rdv_tabBarController] setTabBarHidden:NO];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@""] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage =[[UIImage alloc] init];
    self.navigationController.delegate = self;
    
     
    
    
    if ([[[UIDevice currentDevice] systemVersion] doubleValue] > 8.0)
    {
        //设置定位权限 仅ios8有意义
        [self.locationManager requestWhenInUseAuthorization];// 前台定位
        
        //  [locationManager requestAlwaysAuthorization];// 前后台同时定位
    }
    
    [self.locManager startUpdatingLocation];
    
    _locManager = [[CLLocationManager alloc] init];
    
    _locManager.delegate = self;
    
    _locManager.desiredAccuracy = kCLLocationAccuracyBest;
    
    _locManager.distanceFilter = 1.0f;
    
    
    NSLog(@"%f",_locManager.location.coordinate.longitude);
    NSLog(@"%f",_locManager.location.coordinate.latitude);
    
    
    
    commdity. latitude=[NSString stringWithFormat:@"%f",  _locManager.location.coordinate.latitude];
    
    commdity. longitude=[NSString stringWithFormat:@"%f",_locManager.location.coordinate.longitude];
    
}







- (CLLocationManager *)locationManager{
    if(!_locationManager){
        self.locationManager = [[CLLocationManager alloc] init];
        self.locationManager.delegate = self;
    }
    return _locationManager;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self loadViewTabel];
    
//    [self addFooter];
    
}
//上拉刷新
- (void)addFooter
{
    
    //
    
    
    
    MJRefreshHeaderView* footer = [MJRefreshHeaderView header];
    footer.scrollView = _table;
    footer.beginRefreshingBlock = ^(MJRefreshBaseView* refreshView)
    {
        
        
        
        
        
        [self loadViewTabel];
    
        
        [self performSelector:@selector(doneWithView:) withObject:refreshView afterDelay:3.0f];
        NSLog(@"%@--->正在刷新", refreshView.class);
    };
    
}
- (void)doneWithView:(MJRefreshBaseView *)refreshView
{
    [_table reloadData];
    [refreshView endRefreshing];
}

- (void)loadViewTabel{
    
    NSArray *arr1 = @[@"商户圈",@"shanghuquans"];
    NSArray *arr2 = @[@"资讯",@"zixun"];
    
    NSArray *arr3 = @[@"生意经",@"shengyijings"];
    NSArray *arr4 = @[@"导购培训",@"daogoupeixuns"];
    
    
    NSArray *arr5 = @[@"货源市场",@"huoyuanshichang"];

    
    
    array = @[arr1];
    array1 = @[arr2,arr3,arr4];
    array2 = @[arr5];
    
    



    
    
    
    
    _table.frame=CGRectMake(0, -20, ScreenWidth, ScreenHeight-64-49);
    
    self.table.tableHeaderView = headerView;
    self.table.backgroundColor=zuiqianhuicolor;
}






#pragma mark--TableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3 ;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return array.count;
    }if (section == 1) {
        return array1.count;
    }
    if (section == 2) {
        return array2.count;
    }
    return array3.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    DailyTableViewCell* cell = (DailyTableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        NSArray* nib = [[NSBundle mainBundle] loadNibNamed:@"DailyTableViewCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }
    
    
    
    cell.textLabel.font=[UIFont systemFontOfSize:16];
    
    if (indexPath.section == 0) {
        
        cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",[[array objectAtIndex:indexPath.row] objectAtIndex:1]]];
        cell.textLabel.text = [[array objectAtIndex:indexPath.row] objectAtIndex:0];
        
        
    }if (indexPath.section == 1) {
        
        cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",[[array1 objectAtIndex:indexPath.row] objectAtIndex:1]]];
        cell.textLabel.text = [[array1 objectAtIndex:indexPath.row] objectAtIndex:0];
        
        
    }
    
    if (indexPath.section == 2) {
        cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",[[array2 objectAtIndex:indexPath.row] objectAtIndex:1]]];
        cell.textLabel.text = [[array2 objectAtIndex:indexPath.row] objectAtIndex:0];
        
        
    }
    if (indexPath.section == 3) {
        cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",[[array3 objectAtIndex:indexPath.row] objectAtIndex:1]]];
        cell.textLabel.text = [[array3 objectAtIndex:indexPath.row] objectAtIndex:0];
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    DailyTableViewCell *cell =(DailyTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
    
    if ([cell.textLabel.text isEqualToString:@"商户圈"]) {
        
                NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
        
     userAudit = [user objectForKey:@"userAudit"];
        
        if ([userAudit isEqualToString:@"Y"]) {
            
            
            DiscoverMCircleListViewController * Login=[[DiscoverMCircleListViewController alloc]init];
            
            [self.navigationController pushViewController:Login animated:YES];
            
        }else{
        [self renzhensat];
        
        }
        
        
        

    }else if ([cell.textLabel.text isEqualToString:@"资讯"]) {
        
        
        DiscoverHouseholdListViewController * Login=[[DiscoverHouseholdListViewController alloc]init];
        
        [self.navigationController pushViewController:Login animated:YES];
    }else if ([cell.textLabel.text isEqualToString:@"生意经"]) {
        
        
        DiscoverBusinessListViewController * Login=[[DiscoverBusinessListViewController alloc]init];
        
        [self.navigationController pushViewController:Login animated:YES];
    }else if ([cell.textLabel.text isEqualToString:@"导购培训"]) {
        
        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
        
        userAudit = [user objectForKey:@"userAudit"];
        
        if ([userAudit isEqualToString:@"Y"]) {
        
            DiscoverTrainListViewController * Login=[[DiscoverTrainListViewController alloc]init];
            
            [self.navigationController pushViewController:Login animated:YES];
            
        }else{
            [self renzhensat];
            
        }


           }else if ([cell.textLabel.text isEqualToString:@"货源市场"]) {
        
               NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
               
               userAudit = [user objectForKey:@"userAudit"];
               
               if ([userAudit isEqualToString:@"Y"]) {
                   
                   DiscoverSupplyListViewController * Login=[[DiscoverSupplyListViewController alloc]init];
                   
                   [self.navigationController pushViewController:Login animated:YES];

                   
               }else{
                   [self renzhensat];
                   
               }

            }

    
    
}
-(void)renzhensat{
    
    if ([userAudit isEqualToString:@"E"]) {
        SelectTheCertificationViewController * Login=[[SelectTheCertificationViewController alloc]init];
        
        [self.navigationController pushViewController:Login animated:YES];
    }
    if ([userAudit isEqualToString:@"N"]) {
        SelectTheCertificationViewController * Login=[[SelectTheCertificationViewController alloc]init];
        
        [self.navigationController pushViewController:Login animated:YES];
    }
    if ([userAudit isEqualToString:@"W"]) {
        [self showAlertMessage:@"账号信息审核中，请耐心等待"];
    }
    
    
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return  60;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section==1) {
        return 1;
    }
    return 1;
}

//
//#pragma mark - topBtnAction
//// 知识库
//-(void)xiaoshouhuashu{
//    //只有lan的界面不一样，其他权限的界面一样，所以可以用吧标志为判断不同方法
//    if ([MXUser ifLan]) {
//        [self performSegueWithIdentifier:@"SalesLanViewController" sender:nil];
//    }else{
//        [self performSegueWithIdentifier:@"SaleskillProViewController" sender:nil];
//    }
//}
//
//// 我的培训
//- (void)myTrain{
//    PlanStatisticsViewController *planVC = [[PlanStatisticsViewController alloc] init];
//    planVC.reportPlanData = [[ReportTrainingPlan alloc] init];
//    planVC.reportPlanData.lanId = [MXUser currentUser].lanId;
//    planVC.reportPlanData.areaId = [MXUser currentUser].areaId;
//    planVC.title = @"我的培训";
//    NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];
//    [dateformatter setDateFormat:@"yyyy"];
//    planVC.year = [[dateformatter stringFromDate:[NSDate date]] intValue];
//    [dateformatter setDateFormat:@"MM"];
//    planVC.month = [[dateformatter stringFromDate:[NSDate date]] intValue];
//    planVC.hidesBottomBarWhenPushed = YES;
//    [self.navigationController pushViewController:planVC animated:YES];
//
//}
//
//// 我的测评
//- (void)myExam{
//    UIStoryboard *userCenterStorybord = [UIStoryboard storyboardWithName:@"日常管理_员工测评" bundle:nil];
//    UIViewController *viewontroller;
//    NSString *identifier;
//    if([MXUser ifStaff] || [MXUser ifHall])
//    {
//        identifier = @"RmexamListViewController";
//    }else {
//        identifier = @"RmCmExamListController";
//    }
//    viewontroller = [userCenterStorybord instantiateViewControllerWithIdentifier:identifier];
//    if([MXUser ifHall]){
//        ((RmexamListViewController *)viewontroller).HallifExcm = true;
//    }
//
//    viewontroller.title=@"考试题目一览";
//    viewontroller.hidesBottomBarWhenPushed=YES;//隐藏tabbar
//    [self.navigationController pushViewController:viewontroller animated:YES];
//}
//
//
//// 网点
//- (void)pushHallDot
//{
//    if ([MXUser ifProvince] || [MXUser ifArea] || [MXUser ifLan])
//    {
//        HallDotViewController *hallVC = [[HallDotViewController alloc] init];
//        NSString *type;
//        if ([MXUser ifProvince])
//        {
//            type = @"本地网";
//            hallVC.flag = 0;
//        }
//        else
//        {
//            type = @"县区";
//            hallVC.flag = 1;
//            hallVC.lanId = [MXUser currentUser].lanId;
//        }
//        hallVC.tableViewTopArr = @[type,@"省直控",@"市直控",@"非直控"];
//        hallVC.hidesBottomBarWhenPushed = YES;
//        [self.navigationController pushViewController:hallVC animated:YES];
//    }
//    else
//    {
//        HallStatistics *stati = [[HallStatistics alloc] init];
//        stati.lanId = [MXUser currentUser].lanId;
//        stati.areaId = [MXUser currentUser].areaId;
//        HallListViewController  *listVC = [[HallListViewController alloc] init];
//        listVC.statisticsData = stati;
//        listVC.hidesBottomBarWhenPushed = YES;
//        [self.navigationController pushViewController:listVC animated:YES];
//    }
//}
//
//#pragma mark-- 点击cell的方法
//// 培训计划
//-(void)peixunjihua{
//    UIView *tempView;
//
//    if ([MXUser ifCm])
//        tempView = [[TrainingPlanSingleView alloc] initWithFrame:CGRectMake(0, 10, kDeviceWidth, KDeviceHeight)];
//    else if ([MXUser ifProvince] ||[MXUser ifLan] || [MXUser ifArea])
//        tempView = [[TrainingPlanSegmentedView alloc] initWithFrame:CGRectMake(0, 10, kDeviceWidth, KDeviceHeight)];
//    else return;
//
//    TrainingPlanViewController *trainingPlanVC = [[TrainingPlanViewController alloc] initWithView:tempView];
//
//    [self.navigationController pushViewController:trainingPlanVC animated:YES];
//}
//
//
//
//- (void)changeState{
//    UIActionSheet *actionsheet = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"按创建时间统计" otherButtonTitles:@"按执行时间统计", nil];
//    [actionsheet showInView:[[self.navigationController.viewControllers lastObject] view]];
//}
//
//- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
//{
//
//    [[NSNotificationCenter defaultCenter] postNotificationName:@"statisticsType" object: kIntToString(buttonIndex)];
//}
//
//// 店长培训计划
//- (void)hallPushTimeHallViewControllerWithFlag:(NSInteger)flag
//{
//    TimeHallViewController *execplanProViewController = [[TimeHallViewController alloc]init];
//    execplanProViewController.hidesBottomBarWhenPushed=YES;//隐藏tabbar
//    execplanProViewController.channelId=[MXUser currentUser].channelId;
//    execplanProViewController.flag=flag;
//    if (flag == 0)
//    {
//        execplanProViewController.title=@"促销计划";
//    }
//    else
//    {
//        execplanProViewController.title=@"培训计划";
//    }
//    if ([MXUser ifStaff])
//    {
//        execplanProViewController.staffId=[MXUser currentUser].staffId;//多了一个staffId
//    }
//    [self.navigationController pushViewController:execplanProViewController animated:YES];
//}
//
//// 日常员工测评
//-(void)dianyuanceping{
//    RmexamListViewController *rmexamList = kVCWithStroyboardAndIdentifier(@"日常管理_员工测评", @"RmexamListViewController");
//    rmexamList.title=@"考试题目一览";
//    rmexamList.hidesBottomBarWhenPushed=YES;//隐藏tabbar
//    [self.navigationController pushViewController:rmexamList animated:YES];
//}
//
//
//// 渠道经理测评
//- (void)qudaojingliceping{
//    RmCmExamListController *incentive1ViewController = kVCWithStroyboardAndIdentifier(@"日常管理_员工测评", @"RmCmExamListController");
//    incentive1ViewController.title=@"考试题目一览";
//    incentive1ViewController.hidesBottomBarWhenPushed=YES;//隐藏tabbar
//    [self.navigationController pushViewController:incentive1ViewController animated:YES];
//}
//
//- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
//{
//    if ([segue.identifier isEqualToString:@"TinyClassViewController"]) {
//        TinyClassViewController *tinyClass = segue.destinationViewController;
//        NSInteger index = [sender integerValue];
//        if (index != 0) {
//            tinyClass.seletType = index;
//        }
//    }
//}
- (void)showAlertWait
{
    alt = [[UIAlertView alloc] initWithTitle:@"网络不稳定 ！" message:@"请确认网络" delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:1.0f];
    
}

- (void)showAlertMessage:(NSString *)msg
{
    alt = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:msg delegate:nil cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:1.0f];
}
- (void)hideAlert
{
    if (alt != nil)
    {
        [alt dismissWithClickedButtonIndex:0 animated:YES];
        alt = nil;
    }
}
@end

